package com.pig.service;


import com.alibaba.dubbo.config.annotation.Service;
import com.pig.dao.CondimentDao;
import com.pig.dao.ProductionDao;
import com.pig.dao.RecipesDao;
import com.pig.exception.ServiceException;
import com.pig.pojo.recipes.Condiment;
import com.pig.pojo.recipes.Production;
import com.pig.pojo.recipes.Recipes;
import com.pig.service.recipes.RecipesService;
import com.pig.util.PageObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class RecipesServiceImpl implements RecipesService {
    @Autowired
    private RecipesDao recipesDao;
    @Autowired
    private CondimentDao condimentDao;
    @Autowired
    private ProductionDao productionDao;
    @Override
    public PageObject<Recipes> findObject(String menuName, Integer pageCurrent) {
        if(pageCurrent==null||pageCurrent<1)
            throw new IllegalArgumentException("当前页码值无效");
        int total = recipesDao.getCount(menuName);
        if (total<1)
            throw new ServiceException("查找的菜谱不存在");
        int pageSize=4;
        int indexSize = (pageCurrent-1)*pageSize;
        List<Recipes> records = recipesDao.findObject(menuName,indexSize,pageSize);
        return new PageObject<>(pageSize,total,pageCurrent,records);
    }

    @Override
    public Recipes findObjectById(Integer id) {
        if (id==null)
            throw  new ServiceException("参数错误");
        Recipes cookBook =recipesDao.findObjectById(id);
        //String image = cookBook.getFinishedImg();
        //int index = image.indexOf(",");
        //cookBook.setFinishedImg(image.substring(0, index));
        //System.out.println(cookBook.getFinishedImg());
        return cookBook;
    }
    @Transactional
    @Override
    public void AddRecipes(Recipes recipes) {
        if (recipes==null)
            throw  new ServiceException("参数错误");
        //List<FoodMat> foodMats = new ArrayList<>();
        //List<ProStep> proSteps = new ArrayList<>();
        recipesDao.insertObject(recipes);
        for (Condiment condiment:recipes.getCondiments()){
            condiment.setRecipesId(recipes.getId());
            condimentDao.insertFoodMat(condiment);
            //foodMats.add(foodMat);
        }
        for (Production production:recipes.getProductions()){
            production.setRecipesId(recipes.getId());
            productionDao.insertProStep(production);
            //proSteps.add(proStep);
        }

        System.out.println("添加成功");
    }

    @Override
    public PageObject findObjectPage(String title, Integer pageCurrent) {
        int total = recipesDao.getCount(title);

        return new PageObject(4, total, pageCurrent, null);
    }

    @Transactional
    @Override
    public void deleteById(Long recipesId) {
        productionDao.deleteByRecipesId(recipesId);
        condimentDao.deleteByRecipesId(recipesId);
        recipesDao.deleteById(recipesId);
    }

    @Transactional
    @Override
    public void UpdateRecipes(Recipes recipes) {
        if (recipes==null)
            throw  new ServiceException("参数错误");
        //List<FoodMat> foodMats = new ArrayList<>();
        //List<ProStep> proSteps = new ArrayList<>();
        recipesDao.updateObject(recipes);
        for (Condiment condiment:recipes.getCondiments()){
            condiment.setRecipesId(recipes.getId());
            condimentDao.updateFoodMat(condiment);
            //foodMats.add(foodMat);
        }
        for (Production production:recipes.getProductions()){
            production.setRecipesId(recipes.getId());
            productionDao.updateProStep(production);
            //proSteps.add(proStep);
        }

        System.out.println("更新成功");
    }
}
